//
//  InviteCodeViewController.m
//  tuanGG
//
//  Created by 微他 on 15/12/3.
//  Copyright © 2015年 weita. All rights reserved.
//

#import "InviteCodeViewController.h"

@interface InviteCodeViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *textfield;
@end

@implementation InviteCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(base_gary_bg);
    [self addnavigationTitle:@"验证邀请码" navColor:@"4fd2be"];
    [self initView];
}
- (void)initView {
    _textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 40)];
    _textfield.placeholder = @"请输入别人给你的10位邀请码...";
    _textfield.backgroundColor = [UIColor whiteColor];
    _textfield.delegate = self;
    _textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textfield.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 0)];
    _textfield.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_textfield];


    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(_textfield.frame)+50, ScreenWidth-40, 40)];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    [button addTarget:self action:@selector(buttonBuilds) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
- (void)buttonBuilds {
    if (_textfield.text.length <= 0) {
        [Common addAlert:@"邀请码不能为空"];
    }else {
        [self Networking];
    }
}
- (void)Networking {
    NSDictionary *dictionary = @{@"invitekey":_textfield.text};
    [GLAFRequest startRequest:checkinvitecodetoagent_url paramsDic:dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            [self addAlert:@"验证成功" otherButtonTitles:@"取消"];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.text.length >9) {
        [Common addAlert:@"输入长度不能超过10位"];
        return NO;
    }
    return YES;
}
@end
